<?php

class BackOffice extends Controller {
	
	public function BackOffice(){
	
		parent::Controller();
	
	}
	
	public function login(){
		
		$this->verify();
		
	}
	
	public function verify(){
		
		 if($this->input->post('password')){ //checks whether the form has been submited
		 
		 	$this->load->library('form_validation');//Loads the form_validation library class
			$rules = array(array('field'=>'password','label'=>'password','rules'=>'required'));//validation rules
			 
		 	$this->form_validation->set_rules($rules);//Setting the validation rules inside the validation function
		 
		 	if($this->form_validation->run() == FALSE){ //Checks whether the form is properly sent
		 		 $this->load->view('login_form'); //If validation fails load the login form again
		 	}else{
		 		
				 $result = (md5($this->input->post('password')) == "238be3e025b4e6184dc7e9e7f0da8db5"); //If validation success then call the login function inside the common model and pass the arguments
				 if($result){ //if login success
				 	
					$this->session->set_userdata('logged_in', true); //set the data into the session
					$this->createPage(""); //Load the success page
		 	
				 }else{ // If validation fails.
		 
				 	$data["error"] = "Verkeerde inloggegevens";
				 	$this->load->view('login', $data);
		 			//Load the login page and pass the error message
		 		 }
			 }
			 
		 }else{
		 	$this->session->set_userdata('logged_in', false);
		 	$this->load->view('login');
		 }
		 
	}
	
	
	public function createPage($message = ""){
		
		if($this->session->userdata("logged_in") == 1){
		
		$data["message"] = $message;
		
		$this->db->select('score.ID, Naam, Score, Email, EmailsOntvangen, Datum');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('score.Score', 'desc'); 
		$this->db->order_by('Datum');

		$data["allResults"] = $this->db->get();
		
		$this->db->select('score.ID, Naam, Email, EmailsOntvangen, Datum');
		$this->db->select_max('Score');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('Score', 'desc');
		$this->db->group_by('speler.ID');
		$this->db->limit(10);
		
		$data["topResults"] = $this->db->get();	
		$this->load->view('backoffice', $data);
		
		}else{
		 	$this->session->set_userdata('logged_in', false);
		 	$this->load->view('login');
		 }
		
	}
	
	public function command(){
		
		if($this->session->userdata("logged_in") == 1){

			if($this->input->post('sendMails') != ""){
	
				$this->sendMails();
				
			}else{
				
				$this->createPage("Emails NIET verzonden. Probleem met server");
				
			}
		
		}else{
		 	$this->session->set_userdata('logged_in', false);
		 	$this->load->view('login');
		 }
		
	}
	
	public function remove(){

		if($this->session->userdata("logged_in") == 1){

			if($this->input->post('removeScore') != ""){
	
				$this->db->where('ID', $this->input->post('removeScore'));
				$this->db->delete('score'); 
				
				$this->createPage("Score gewist");
				
			}else{
				
				$this->createPage("Probleem tijdens het verwijderen");
				
			}
		
		}else{
		 	$this->session->set_userdata('logged_in', false);
		 	$this->load->view('login');
		 }
	
	}
	
	public function sendMails(){
		
		if($this->session->userdata("logged_in") == 1){
		
		$this->load->library('email');
		$config['mailtype'] = 'html';

		$this->email->initialize($config);
		
		$this->db->select('speler.ID, Naam, Email, EmailsOntvangen');
		$this->db->select_max('Score');
		$this->db->from('score');
		$this->db->join('speler', 'score.SpelersID = speler.ID');
		$this->db->order_by('Score', 'desc');
		$this->db->order_by('Datum');
		$this->db->group_by('speler.ID');
		//$this->db->limit(2);
		
		$query = $this->db->get();
		$count = 0;
		
		foreach($query->result() as $row){
			
			if ($row->EmailsOntvangen){
			
				$this->email->clear();
	
				$this->email->from('info@festivaldranouter.be', 'Festival Dranouter');
				$this->email->to($row->Email);
				
				$this->email->subject('Verbeter uw score!');
				
				$count++;
				$data['plaats'] = $count;
				$data['punten'] = $row->Score;
				$data['naam'] = $row->Naam;
				
				$email = $this->load->view('mail', $data, TRUE);
				
				$this->email->message($email);
				
				$this->email->send();
			
			}
		
		}
		
		$this->createPage("Emails verzonden");
		
		}else{
		 	$this->session->set_userdata('logged_in', false);
		 	$this->load->view('login');
		 }
		
	}
	
}